package edu.xidian.forum.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import edu.xidian.forum.entity.Favorite;
import edu.xidian.forum.entity.User;
import edu.xidian.forum.mapper.FavoriteMapper;
import edu.xidian.forum.service.FavoriteService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;


@Service
@Transactional(rollbackFor = Exception.class)
public class FavoriteImpl extends ServiceImpl<FavoriteMapper, Favorite> implements FavoriteService {

    @Override
    public Favorite like(User user, String topic_id) {
        // 封装

        Favorite favorite = new Favorite();
        favorite.setTopicId(topic_id);
        favorite.setUserId(user.getId());
        this.baseMapper.insert(favorite);
        return favorite;
    }

    @Override
    public void unlike(Favorite favorite) {
        // 封装
        this.baseMapper.delete(new LambdaQueryWrapper<Favorite>().eq(Favorite::getId, favorite.getId()));
        return;
    }

    @Override
    public Favorite hasliked(User user, String topic_id) {

        return this.baseMapper.selectOne(new LambdaQueryWrapper<Favorite>().eq(Favorite::getUserId, user.getId())
                .eq(Favorite::getTopicId, topic_id));
    }
}
